/*
 * @lc app=leetcode.cn id=69 lang=javascript
 *
 * [69] Sqrt(x)
 */

// @lc code=start
/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function (x) {
  if (x <= 1) {
    return x;
  }

  let min = 1;
  let max = x;
  let half = null;
  while (min < max - 1) {
    half = Math.floor((min + max) / 2);
    const temp = half * half;
    if (temp < x) {
      min = half;
    } else if (temp > x) {
      max = half;
    } else {
      return half;
    }
  }
  return min;
};
// @lc code=end
